package com.ermace.robo.jumper.game.GameObjects;

import android.graphics.Rect;
import android.os.Parcel;
import android.os.Parcelable;

import com.ermace.robo.jumper.MainActivity;
import com.ermace.robo.jumper.game.Line;
import com.ermace.robo.jumper.game.Player.Position;
import com.ermace.robo.jumper.game.Player.Robot;

public abstract class GameObject implements Parcelable {

	private int PICTURE_ID;
	protected Position position;
	protected Position pxPosition;
	protected double length;
	protected double height;
	
	protected GameObject(double x, double y, double length, int picId)
	{
		PICTURE_ID = picId;
		position = new Position(x, y);
		pxPosition = null;
		this.length = length;
	}
	
	protected GameObject(Parcel in) {
		readFromParcel(in);
	}
	
	public int getPictureId() {
		return PICTURE_ID;
	}
	
	public abstract Rect getRect();
	
	public Position getPosition()
	{
		return position;
	}
	
	public Position getPxPosition()
	{	
		if(pxPosition == null)
			pxPosition = new Position(position.getX() * MainActivity.getScreenWidth() / 30, position.getY() * MainActivity.getScreenHeight() / 20);
		return pxPosition;
	}
	
	public double getPxLength()
	{
		return length * MainActivity.getScreenWidth() / 30;
	}
	public double getPxHeight()
	{
		return MainActivity.getScreenWidth() / 20;
	}
	public abstract boolean contains(Position pos);
	
	/**
	 * 
	 * @param line
	 * 		  The line to check collision with the top of the rectangle
	 * @result The Position of the collision.
	 * 		   Null if there is no collision.
	 */
	public abstract Position collide(Line line);
	

	public abstract void interact(Robot robot);
	public abstract void update(double timeIntervalMS);
	
	/**
    *
    * This field is needed for Android to be able to
    * create new objects, individually or as arrays.
    *
    * This also means that you can use use the default
    * constructor to create the object and use another
    * method to hyrdate it as necessary.
    *
    */
	public static final Parcelable.Creator<GameObject> CREATOR
			= new Parcelable.Creator<GameObject>() {
		@Override
		public GameObject createFromParcel(Parcel in) {
			String derivedClassName = in.readString();
			if (derivedClassName.equals(Rectangle.class.getName())) {
				return new Rectangle(in);
			} else if (derivedClassName.equals(Ice.class.getName())) {
				return new Ice(in);
			}
			
			return null;
		}

		@Override
		public GameObject[] newArray(int size) {
			return new GameObject[size];
		}
	};
	
	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		
		dest.writeInt(PICTURE_ID);
		
		dest.writeDouble(position.getX());
		dest.writeDouble(position.getY());
		
		dest.writeDouble(length);
	}
	
	private void readFromParcel(Parcel in) {
		
		PICTURE_ID = in.readInt();
		
		position = new Position(in.readDouble(), in.readDouble());
		
		length = in.readDouble();
	}
}
